<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        ul{
            padding: 0;
        }
      .item {
        margin: 10px 0;
        padding: 10px;
        background: #eee;
      }
      .title{
          font-size: 24px;
          margin-top: 40px;
      }
    </style>
  </head>
  <body>
    <p class="title">for循环事件</p>
    <ul>
      <li class="item">1</li>
      <li class="item checked">2</li>
      <li class="item checked">3</li>
      <li class="item">4</li>
      <li class="item">5</li>
    </ul>
    <p class="title">for循环绑定事件-闭包</p>
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <p class="title">for循环let绑定事件</p>
    <dl>
      <dd class="item">1</dd>
      <dd class="item">2</dd>
      <dd class="item">3</dd>
      <dd class="item">4</dd>
      <dd class="item">5</dd>
    </登录>
    <script>
      var liEles = document.getElementsByTagName("li");
      for (var i = 0; i < liEles.length; i++) {
        liEles[i].addEventListener("click", function () {
          alert(i); // 都是5
        });
      }
      var divEles = document.getElementsByTagName("div");
      for (var j = 0; j < divEles.length; j++) {
        (function (j) {
          divEles[j].addEventListener("click", function () {
            alert(j); // 0-4
          });
        })(j);
      }
      var ddEles = document.getElementsByTagName("dd");
      for (let k = 0; k < ddEles.length; k++) {
        ddEles[k].addEventListener("click", function () {
            alert(k); // 0-4
        });
      }     
    </script>
  </body>
</html>
